package com.alpha.eceasy.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.mall.api.entity.MallCustomer;
import com.alpha.eceasy.mall.api.entity.MallOrder;
import com.alpha.eceasy.mall.mapper.MallCustomerMapper;
import com.alpha.eceasy.mall.mapper.MallOrderMapper;
import com.alpha.eceasy.mall.service.MallOrderDetailService;
import com.alpha.eceasy.mall.api.entity.MallOrderDetail;
import com.alpha.eceasy.mall.api.vo.MallOrderDetailVO;
import com.alpha.eceasy.mall.mapper.MallOrderDetailMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.io.Serializable;
import java.util.ArrayList;

import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wdc
 */
@Service
@RequiredArgsConstructor
public class MallOrderDetailServiceImpl extends BaseServiceImpl<MallOrderDetailMapper, MallOrderDetail>
        implements MallOrderDetailService {
    @Resource
    private MallOrderMapper orderMapper;
    @Resource
    private MallCustomerMapper customerMapper;

    @Override
    public List<MallOrderDetailVO> getMallOrderDetailVOList(MallOrderDetail mallOrderDetail) {
        List<MallOrderDetail> mallOrderDetailList = this.list(Wrappers.lambdaQuery(mallOrderDetail));
        List<MallOrderDetailVO> mallOrderDetailVOList = new ArrayList<>();
        if (!mallOrderDetailList.isEmpty()) {
            for (MallOrderDetail orderDetail : mallOrderDetailList) {
                MallOrderDetailVO mallOrderDetailVO = BeanUtil.copyProperties(orderDetail, MallOrderDetailVO.class);
//				//如果商品主表名称为空则取商品规格名称
//				if (StrUtil.isBlank(orderDetail.getCommodityName())){
//					mallOrderDetailVO.setCommodityName(orderDetail.getCommoditySpecificationName());
//				}
//				//查询商品规格单价和规格图片
//				if (orderDetail.getCommoditySpecificationId() != null) {
//					MallCommoditySpecification mallCommoditySpecification = mallCommoditySpecificationMapper.selectById(orderDetail.getCommoditySpecificationId());
//					if (Objects.nonNull(mallCommoditySpecification)) {
//						mallOrderDetailVO.setSpecificationCover(mallCommoditySpecification.getCover());
//						mallOrderDetailVO.setSpecificationPrice(mallCommoditySpecification.getPrice());
//					}
//				}
                mallOrderDetailVOList.add(mallOrderDetailVO);
            }
        }
        return mallOrderDetailVOList;
    }

    public MallOrderDetailVO getVOByGuid(Serializable guid) {
        MallOrderDetail orderDetail = super.getByGuid(guid);
        MallOrderDetailVO vo = new MallOrderDetailVO(orderDetail);
        MallOrder order = orderMapper.selectByGuid(orderDetail.getOrderGuid());
        MallCustomer customer = customerMapper.selectByGuid(order.getCustomerGuid());
        vo.setCustomerName(customer.getName());
        return vo;
    }
}
